package com.epoint.mobileoa.personalized.epointcompany.ui.gzrz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.content.Intent;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.personalized.epointcompany.model.CheckUserInfoModel;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.DateHelp;
import com.epoint.mobileoa.tools.PhoneHelp;

/**
 * @author liyc
 * @time 2012-8-27 下午6:20:11
 * @annotation 
 */
public class RizhiShenHeDetailView extends SuperView {

	private List<CheckUserInfoModel> list;
	private String rzdate;

	public static int backTag = 0;//0resume不刷新数据,1刷新数据
	@Override
	public void initControls() {

		rzdate = getIntent().getStringExtra("rzdate");
		String week = DateHelp.getWeekByDateStr(rzdate);
		TextView tvDate = (TextView)findViewById(R.id.tvDate);
		tvDate.setText(rzdate+"("+week+")");
		initData();
	}

	private void initData()
	{
		list = new ArrayList<CheckUserInfoModel>();
		showDialog_wait(null);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("rzdate", rzdate);
		Task task = new GetCanCheckUserListTask(this, param);
		MainService.addTask(task);
	}

	@Override
	public void refresh(Object... param) {
		m_Dialog.dismiss();
		if(param[0].toString().equals(MainService.NORMAL)){
			String bs = param[1].toString();
			list = GetCanCheckUserListTask.XmlAnalysis("<root>"+bs+"</root>");
			List<CheckUserInfoModel> sqlist = new ArrayList<CheckUserInfoModel>();
			for(CheckUserInfoModel c:list)
			{
				if(c.Flag.equals("sq"))
				{
					sqlist.add(c);
				}
			}

			LinearLayout llbs = (LinearLayout) findViewById(R.id.llBsry);
			setPersonBtn(sqlist, llbs);

			List<CheckUserInfoModel> bmlist = new ArrayList<CheckUserInfoModel>();
			for(CheckUserInfoModel c:list)
			{
				if(c.Flag.equals("bm"))
				{
					bmlist.add(c);
				}
			}
			LinearLayout llbm = (LinearLayout) findViewById(R.id.llBmry);

			setPersonBtn(bmlist, llbm);

			List<CheckUserInfoModel> xmlist = new ArrayList<CheckUserInfoModel>();
			for(CheckUserInfoModel c:list)
			{
				if(c.Flag.equals("xm"))
				{
					xmlist.add(c);
				}
			}

			LinearLayout llxm = (LinearLayout) findViewById(R.id.llXmry);
			setPersonBtn(xmlist, llxm);
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if(backTag==1)
		{
			initData();
		}
	}

	private void setPersonBtn(List<CheckUserInfoModel> chechlist,LinearLayout llFrame)
	{
		llFrame.removeAllViews();
		int rowNums = 4;
		int bmnumRow = chechlist.size()/rowNums+1;
		int phoneWidth =  PhoneHelp.getPhoneWidth(this);
		int btnWidth = phoneWidth/4-5;

		for(int i=0;i<bmnumRow;i++)
		{
			LinearLayout ll = new LinearLayout(this);
			for(int j=0;j<rowNums;j++)
			{

				int position = i*rowNums+j;
				if(position<=chechlist.size()-1)
				{
					CheckUserInfoModel c = chechlist.get(position);

					Button btn = new Button(this);
					btn.setOnClickListener(new ClickPerson());
					String ysTag = "";
					String nameColor = "black";
					if(c.IsCheck.equals("3"))
					{
						nameColor = "red";
						btn.setClickable(false);
						btn.setEnabled(false);
					}
					else if(c.IsCheck.equals("1"))
					{
						nameColor = "blue";
						if(c.RZPSDate.trim().length()>0)
						{
							ysTag = "<font color=red>√</font>";
						}
					}
					else if(c.IsCheck.equals("2"))
					{
						nameColor = "black";
						ysTag = "<font color=red>√</font>";
					}
					//btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn));
					btn.setGravity(Gravity.CENTER);
					btn.setWidth(btnWidth);
					btn.setHeight(btnWidth);
					btn.setText(Html.fromHtml("<font color="+nameColor+">"+c.UserName+"</font>"+ysTag));
					btn.setTag(c);

					ll.addView(btn);
				}
			}
			llFrame.addView(ll);
		}
	}

	class ClickPerson implements OnClickListener
	{

		@Override
		public void onClick(View v) {
			CheckUserInfoModel c=  (CheckUserInfoModel)v.getTag();
			backTag = 0;
			//ToastHelp.showToast(RizhiShenHeDetailView.this, c.UserName);
			Intent intent = new Intent(RizhiShenHeDetailView.this, SinglePersonCheck.class);
			intent.putExtra("UserGuid", c.UserGuid);
			intent.putExtra("UserName", c.UserName);
			intent.putExtra("RZGuid", c.RZGuid);
			intent.putExtra("RZDate", rzdate);
			startActivity(intent);
		}

	}

	@Override
	public int getLayoutInt() {
		return R.layout.rizhishenhedetailview;
	}

	@Override
	public String setTitle() {
		return "工作日志审核";
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		backTag = 0;
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param){
	}
}
